"use strict";
//全局变量们
var resource="";//原始数据
var fillresource="";//填充数据
var currentpage=1,totalpage=1;//当前页、总页数
var partpagenum=10;//每页显示行数

// 初始化
$(function () {
	//获取数据
	$.getJSON("./lang/trainingresource.json",function(data){
		resource=data;
		sortdata();
		settable(currentpage);
	});

	// 输入提示
	$("#queryResourcestring").attr("placeholder", langThis("px_seacherholder"));
	/*查询框内容变化事件绑定*/
	$("#queryResourcestring").change(filterlesson_click);
});
// 查询下拉按钮点击
function getQueryname(str)
{
	var re = str ? new RegExp(str) : /.*/;
	var lis = "";
	for (var i in resource.data) {
		if (re.test(langThis(resource.data[i].name))) {
			lis += '<li>' + langThis(resource.data[i].name) + '</li>';
		}
	}
	$("ul.dropdown-menu.querymenu").html(lis);
	//无匹配项提示
	if (!$("ul.dropdown-menu.querymenu>li").length) {
		$("ul.dropdown-menu.querymenu").append(langThis("kc_nomatches"));
	}

	/*绑定查询下拉列表点击事件*/
	$("ul.dropdown-menu.querymenu>li").click(function () {
		$("#queryResourcestring").val($(this).text());
		filterlesson_click();
	});
}
// 输入时显示过滤
function completer_click()
{
	getQueryname($("#queryResourcestring").val());
	$("#inputlessoncontentdiv").attr("class", "input-group-btn open");
}
//显示过滤内容
/*过滤显示内容*/
function filterlesson_click()
{
	fillresource = [];
	var re = new RegExp($("#queryResourcestring").val());
	for (var i in resource.data) {
		if (re.test(langThis(resource.data[i].name))) {
			fillresource.push(resource.data[i]);
		}
	}
	settable(currentpage);
}
// 填充表格
function settable(page) {
	fillresource=!fillresource || !fillresource.length ? resource.data:fillresource;
	//设置页码
	totalpage=Math.ceil(fillresource.length/partpagenum);
	currentpage=page>totalpage?totalpage:page;

	var content = "";
	// 填充表格
	for (var i = (currentpage - 1) * partpagenum; i < fillresource.length; i++)
	{
		var line = "<tr>"
		line += '<td>' + (parseInt(i) + 1) + '</td>';
		line += '<td class="resourceimg"><img src="' + fillresource[i].typeurl + '"/></td>';
		line += '<td class="resourcelink"><p data-toggle="tooltip" data-placement="top" title="' + langThis(fillresource[i].nametips) + '"><a role="button" class="btn btn-link" target="_black" href="' + fillresource[i].source + '">' + langThis(fillresource[i].name) + '</a></p></td>';
		line += '<td>' + langThis(fillresource[i].form) + '</td>';
		content += line;

		if(i==currentpage * partpagenum-1){break;}
	}
	$("#resourcesbody").html(content);
	$('#resourcesbody p[data-toggle="tooltip"]').tooltip();

	//设置页码
	setpartpagebar();
    // 子页面设置父页面iframe高度
    if(window.parent.setiframeHeight){
		var ht=$("#container").height();
		ht=ht>800?ht:800;
        window.parent.setiframeHeight(ht);
    }
}
//排序点击
function sortbayfrom()
{
	if ($("#sortlines").attr("data-pos") === "1") {
		$("#sortlines").attr("data-pos", "0");
		sortdata();
		settable(currentpage);
		$("#sortlines").html('<span class="glyphicon glyphicon-triangle-bottom"></span>' + langThis("px_resourcewhere"));
	} else {
		$("#sortlines").attr("data-pos", "1");
		sortdata();
		settable(currentpage);
		$("#sortlines").html('<span class="glyphicon glyphicon-triangle-top"></span>' + langThis("px_resourcewhere"));
	}
}
//排序
function sortdata()
{
	if ($("#sortlines").attr("data-pos") === "1") {
		resource.data.sort(function (a, b) {
			if (langThis(a.form) == langThis(b.form)) {
				return langThis(a.name) > langThis(b.name) ? 1 : -1;
			} else {
				return langThis(a.form) > langThis(b.form) ? 1 : -1;
			}
		});
	} else if ($("#sortlines").attr("data-pos") === "0") {
		resource.data.sort(function (a, b) {
			if (langThis(a.form) == langThis(b.form)) {
				return langThis(a.name) > langThis(b.name) ? -1 : 1;
			} else {
				return langThis(a.form) > langThis(b.form) ? -1 : 1;
			}
		});
	}
}
//分页控制
function setpartpagebar(){
	var pagesli='';
	//首页、上一页
	if(currentpage==1 || totalpage==1){
		pagesli+='<li title="'+langThis("nav_master")+'" class="active"><a>&laquo;</a></li>';
		pagesli+='<li title="'+langThis("nav_pre")+'" class="active"><a>&lt;</a></li>';
	}else{
		pagesli+='<li title="'+langThis("nav_master")+'"><a style="cursor:pointer;" onclick="gotofirstpage()">&laquo;</a></li>';
		pagesli+='<li title="'+langThis("nav_pre")+'"><a style="cursor:pointer;" onclick="previouspage()">&lt;</a></li>';
	}
	//页码
	var startpage;
	if (currentpage-5<=0){
		startpage=1;
	}else if(currentpage+5>totalpage){
		startpage=totalpage-9;
	}else{
		startpage=currentpage-4;
	}
	// 左过长
	if (startpage>1){
		pagesli += '<li><a style="cursor:pointer;" onclick="gotopage(' + (startpage-1) + ')">...</a></li>';
	}else{
		startpage=1;
	}
	for(var j=startpage;j<=totalpage;j++){
		if(j==currentpage){
			pagesli+='<li class="active"><a>'+j+'</a></li>';
		}else{
			pagesli+='<li><a style="cursor:pointer;" onclick="gotopage('+j+')">'+j+'</a></li>';
		}
		//右过长
		if((j==startpage+9) && (j<totalpage)){
			pagesli+='<li><a style="cursor:pointer;" onclick="gotopage('+(startpage+10)+')">...</a></li>';
			break;
		}
	}
	//下一页、尾页
	if(currentpage==totalpage || totalpage==1){
		pagesli+='<li title="'+langThis("nav_next")+'" class="active"><a>&gt;</a></li>';
		pagesli+='<li title="'+langThis("nav_last")+'" class="active"><a>&raquo;</a></li>';
	}else{
		pagesli+='<li title="'+langThis("nav_next")+'"><a style="cursor:pointer;" onclick="nextpage()">&gt;</a></li>';
		pagesli+='<li title="'+langThis("nav_last")+'"><a style="cursor:pointer;" onclick="gotolastpage()">&raquo;</a></li>';
	}
	$("#partpagenavbar>ul.pagination").html(pagesli);
	$("#partpagenavbar").fadeIn();
}
function previouspage(){
	settable(--currentpage);
}
function nextpage(){
	settable(++currentpage);
}
function gotopage(pagenum){
	settable(pagenum);
}
function gotofirstpage(){
	settable(1);
}
function gotolastpage(){
	settable(totalpage);
}
// 翻页快捷键
document.onkeydown = function(e){
    if((e||event).keyCode==37 && currentpage>1){//上一页
		previouspage();
	}else if((e||event).keyCode==39 && currentpage<totalpage){//下一页
		nextpage();
	}else if((e||event).keyCode==36 && currentpage !=1){//首页
		gotofirstpage();
	}else if((e||event).keyCode==35 && currentpage != totalpage){//尾页
		gotolastpage();
	}
};